package {{.pkg}}

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"time"

	"orchestrator/internal/infra/database/mysql/po"
    "orchestrator/internal/infra/filter"
	jrpc "orchestrator/internal/infra/jsonrpc"

	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type Time time.Time

func (t Time) MarshalJSON() ([]byte, error) {
	timestamp := time.Time(t).Format("2006-01-02 15:04:05")
	return json.Marshal(timestamp)
}

func (t *Time) UnmarshalJSON(b []byte) error {
	var v string
	if err := json.Unmarshal(b, &v); err != nil {
		return err
	}
	parse, err := time.Parse("2006-01-02 15:04:05", v)
	if err != nil {
		return err
	}
	*t = Time(parse)
	return nil
}

type {{.upperStartCamelObject}} struct {
	{{.fields}}
}

func RegisterMethod(conn sqlx.SqlConn) {
	genJsonFile()
	genFilterFile()

	m := po.New{{.upperStartCamelObject}}Model(conn)
	{{.codes}}
}


func makeMockData() *{{.upperStartCamelObject}} {
	c := {{.upperStartCamelObject}}{
			{{.fieldsMock}}
	}
	return &c
}


func makeFilterData() *filter.QueryRequest {

	exprs := []filter.FilterElement{}

	expr := filter.NewQueryExpression("CreatedAt", filter.Gt, time.Now().AddDate(0, 0, -1))
	exprs = append(exprs, expr)

	expr = filter.NewQueryExpression("CreatedAt", filter.Gte, time.Now())
	exprs = append(exprs, expr)

	expr = filter.NewQueryExpression("CreatedAt", filter.Eq, time.Now())
	exprs = append(exprs, expr)

	expr = filter.NewQueryExpression("CreatedAt", filter.Ne, time.Now())
	exprs = append(exprs, expr)

	expr = filter.NewQueryExpression("CreatedAt", filter.Lt, time.Now())
	exprs = append(exprs, expr)

	expr = filter.NewQueryExpression("CreatedAt", filter.Lte, time.Now())
	exprs = append(exprs, expr)

	expr = filter.NewQueryExpression("Status", filter.Range, []any{1, 10}...)
	exprs = append(exprs, expr)

	expr = filter.NewQueryExpression("Name", filter.Like, time.Now().AddDate(0, 0, -1))
	exprs = append(exprs, expr)

	expr1 := filter.NewQueryExpression("Value", filter.In, []any{"a", "b", "c"}...)

	filter1 := filter.NewAndFilter(exprs...)
	filter2 := filter.NewOrFilter(filter.FilterElement(expr1))
	fs := filter.NewAndFilter(filter1, filter2)

	ss := []*filter.Sort{filter.NewSort("CreatedAt", filter.DescOrder)}

	p := filter.NewPagination(1, 10)
	g := filter.NewGroup("CreatedAt", "Kind")
	return filter.NewQueryRequest(fs, g, ss, p)
}

func genJsonFile() {
	// 生成模拟数据

	os.MkdirAll("jsons", os.ModePerm)
	fileName := "{{.upperStartCamelObject}}.json"
	path := filepath.Join("jsons", fileName)
	_, err := os.Stat(path)
	if os.IsNotExist(err) {
		// 将数据写入文件
		file, err := os.Create(path)
		if err != nil {
			fmt.Println("Error creating file:", err)
			return
		}
		defer file.Close()

		encoder := json.NewEncoder(file)
		encoder.SetIndent("", "  ")

		c := makeMockData()
		err = encoder.Encode(c)
		if err != nil {
			fmt.Println("Error encoding JSON:", err)
			return
		}

		fmt.Println("Data written to {{.upperStartCamelObject}}.json")
	} else {
		fmt.Println("File", path, "already exists.")
	}
}

func genFilterFile() {
	// 生成模拟数据

	os.MkdirAll("jsons", os.ModePerm)
	fileName := "{{.upperStartCamelObject}}_filter.json"
	path := filepath.Join("jsons", fileName)
	_, err := os.Stat(path)
	if os.IsNotExist(err) {
		// 将数据写入文件
		file, err := os.Create(path)
		if err != nil {
			fmt.Println("Error creating file:", err)
			return
		}
		defer file.Close()

		encoder := json.NewEncoder(file)
		encoder.SetIndent("", "  ")

		c := makeFilterData()
		err = encoder.Encode(c)
		if err != nil {
			fmt.Println("Error encoding JSON:", err)
			return
		}

		fmt.Println("Data written to {{.upperStartCamelObject}}_filter.json")
	} else {
		fmt.Println("File", path, "already exists.")
	}
}